﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2025-03-01 16:45
    /// 描 述：服务类
    /// </summary>
    public class AttributesService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<AttributesEntity>> GetList(AttributesListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            foreach (var item in list)
            {
                var valueslist = this.BaseRepository().FindList<AttributeValuesEntity>(t => t.attribute_id == item.Id).Result.Select(t => t.value);
                if (valueslist.Count()>0)
                {
                    item.values = string.Join(";", valueslist);
                }
            }
            return list.ToList();
        }

        public async Task<List<AttributesEntity>> GetPageList(AttributesListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);

            foreach (var item in list)
            {
                var valueslist = this.BaseRepository().FindList<AttributeValuesEntity>(t => t.attribute_id == item.Id).Result.Select(t => t.value);

                if (valueslist.Count() > 0)
                {
                    item.values = string.Join(";", valueslist);
                }
            }

            return list.ToList();
        }

        public async Task<AttributesEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<AttributesEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(AttributesEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
            string[] values = TextHelper.SplitToArray<string>(entity.values, ';');

            var valueslist = this.BaseRepository().FindList<AttributeValuesEntity>(t => t.attribute_id == entity.Id).Result.Select(t => t.value);

            if (valueslist.Count()>0)
            {
                await this.BaseRepository().Delete(valueslist);
            }

            foreach (var item in values)
            {
                AttributeValuesEntity valuesEntity = new AttributeValuesEntity();
                valuesEntity.value = item;
                valuesEntity.attribute_id = entity.Id;
                valuesEntity.Create();
                await this.BaseRepository().Insert(valuesEntity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<AttributesEntity>(idArr);

            var valueslist = this.BaseRepository().FindList<AttributeValuesEntity>(t => idArr.Contains(t.attribute_id.Value)).Result;
            if (valueslist.Count()> 0)
            {
                await this.BaseRepository().Delete(valueslist);
            }
        }
        #endregion

        #region 私有方法
        private Expression<Func<AttributesEntity, bool>> ListFilter(AttributesListParam param)
        {
            var expression = LinqExtensions.True<AttributesEntity>();
            if (param != null)
            {
                if (!param.category_id.IsNullOrZero()) 
                {
                    expression = expression.And(t => t.category_id == param.category_id);
                }
            }
            return expression;
        }
        #endregion
    }
}
